package com.appcenter.application.mapper;


import com.appcenter.application.dto.apps.AppDto;
import com.appcenter.application.dto.permissions.PermissionsDto;
import com.appcenter.application.input.app.GetAppsInput;
import com.appcenter.application.model.Apps;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;

@org.apache.ibatis.annotations.Mapper
@Repository
public interface AppsMapper extends Mapper<Apps>{

    @Select("select * from apps where AppCode=#{AppCode} and ValidStatus=1")
    public List<Apps> GetAllAppsByAppCode(String AppCode);

    @Select("select * from apps where ValidStatus=1 and AppCode=#{AppCode} AND id <> #{NewId}")
    public List<Apps> GetAllAppsByAppCodeNoId(@Param("AppCode") String AppCode,@Param("NewId") String NewId);


    @Select("select * from apps where ValidStatus=1 and AppName=#{AppName}")
    public List<Apps> GetAllAppsByAppName(String AppName);

    @Select("select * from apps where ValidStatus=1 and AppName=#{AppName} AND id <> #{NewId}")
    public List<Apps> GetAllAppsByAppNameNoId(@Param("AppName") String AppName,@Param("NewId") String NewId);


    @Select({
            "<script>",
            "select * from apps where id in",
            "<foreach collection='appIds' item='appId' open='(' separator=',' close=')'>",
            "#{appId}",
            "</foreach>",
            "order by orderList",
            "</script>"
    })
    public List<AppDto> GetAppsByAppIds(@Param("appIds") List<String> appIds);

    @Select("select * from apps where AppCode=#{AppCode} and ValidStatus=1")
    public Apps GetAppsByAppCode(String AppCode);





    @Select("<script>" +
            "select apps.* from apps  " +
            "inner join (select id from (select distinct p.id from permissions p " +
            "inner JOIN getpermissions g on p.Id=g.permissionId " +
            "inner join (select distinct e.Id from orgs e " +
            "inner join (select a.JobId,a.OrgId,c.OrgIdPath from orgjobs a " +
            "inner join jobusers b on a.Id=b.OrgJobId " +
            "inner join orgs c on a.OrgId=c.Id " +
            "inner join dataversion d on d.VersionNo=b.VersionNo and d.VersionNo=a.VersionNo  and d.VersionNo=c.VersionNo and d.VersionNo=b.VersionNo " +
            "where b.UserId=#{userCode}) f " +
            "inner join dataversion d on d.VersionNo=e.VersionNo " +
            "where f.OrgIdPath like concat(e.OrgIdPath,'%') " +
            "UNION all " +
            "select o.jobid from jobusers p  " +
            "inner join orgjobs o on p.orgjobid=o.id " +
            "inner join dataversion d on  d.VersionNo=p.VersionNo and  d.VersionNo=o.VersionNo " +
            "where UserId=#{userCode} " +
            "union ALL " +
            "select Roleid from roleusers where UserId=#{userCode} " +
            "union ALL " +
            "select i.id from users i " +
            "inner join dataversion d on d.VersionNo=i.VersionNo " +
            "where UserCode=#{userCode}) u on u.id=g.dataId  " +
            "and( p.AppId is null or p.AppId=(select Id from apps where appCode=#{appCode})) and g.AppCode=#{appCode} " +
            "and p.ValidStatus=1 and p.IsVisible=1 and PermissionType='应用' " +
            ") temp) pes on apps.id=pes.id  where ValidStatus=true " +
            "" +
            " <when test='keyword!=null'>" +
            " and (AppCode like concat('%',#{keyword},'%') or AppName like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            "" +
            "" +
            "" +
            "" +
            "" +
            "</script>")
    public List<Apps> GetApps(GetAppsInput input);







}
